﻿using System;
using System.Collections.Generic;
using System.Linq;
using Crosland.Automotriz.Logic.Linq;
using DevExpress.Web.ASPxEditors;
using System.Web.UI;
using System.Configuration;
using System.Web.UI.WebControls;

namespace Crosland.Automotriz.Web
{
    public partial class wFrmCliente : Page
    {
        readonly clsTD_VendedorBL bl = new clsTD_VendedorBL();
        readonly clsTH_VentaBL blVenta = new clsTH_VentaBL();
        string idGestionComercial = ConfigurationManager.AppSettings["GestionComercial"];
        const string script = "MostrarAlerta();";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                CargarGrillaClientes();
                Panel img = (Panel)Master.FindControl("asp");
                img.Visible = false;
                if (Request.QueryString["busqueda"] != null)
                {
                    string busqueda = Request.QueryString["busqueda"];
                    if (busqueda.Trim() != "")
                    {
                        txtDescripcion.Text = busqueda;
                        btnConsultar_Click(null, null);
                    }
                }
            }
        }

        private void CargarGrillaClientes()
        {
            string des_cliente = txtDescripcion.Text.Trim();
            int filas = int.Parse(cboOpcion.SelectedItem.Value.ToString());

            grdClientes.DataSource = blVenta.ListarClientes(des_cliente, filas);
            grdClientes.DataBind();
            grdClientes.FocusedRowIndex = -1;
        }

        protected void btnSalir_Click(object sender, EventArgs e)
        {
            Response.Redirect("wFrmDefault.aspx?id=" + idGestionComercial);
        }

        protected void btnConsultar_Click(object sender, EventArgs e)
        {
            CargarGrillaClientes();
        }

        protected void grdClientes_PageIndexChanged(object sender, EventArgs e)
        {
            CargarGrillaClientes();
        }

        protected void btnEditar_Click(object sender, EventArgs e)
        {
            int indice = grdClientes.FocusedRowIndex;
            if (indice >= 0)
            {
                List<object> keys = grdClientes.GetSelectedFieldValues(new string[] { grdClientes.KeyFieldName });
                string idCliente = keys[0].ToString();

                Response.Redirect("wFrmClienteRegistrar.aspx?idCliente=" + idCliente + "&id=" + idGestionComercial + "&busqueda=" + txtDescripcion.Text.Trim());
            }
        }

        protected void btnNuevo_Click(object sender, EventArgs e)
        {
            Response.Redirect("wFrmClienteRegistrar.aspx?id= " + idGestionComercial);
        }

    }
}